package baseclass;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Properties;

import iptux.Utils;

/**
 * Configuration save and load class. config file saves in the user home, name
 * is "iptux.properties".
 * 
 * @author cwll<cwll2009@126.com>
 *
 */
public class Configer {
	
	public String msgEncoding = "GB18030";
	public boolean enterKeySend = true;
	public boolean saveChatHistory = false;
	public String fileSavePath="";
	public String userName="";
	public String groupName="";
	public String icon = "";
	public String[] broadcastRanges;
	public int 	midSplitPostion = 130;
	public int 	leftSplitPostion = 150;
	
	Properties configer;
	String configFilePath;
	
	public Configer(){
		configFilePath = System.getProperty("user.home");
		configFilePath = configFilePath + File.separator + "iptux.properties";
	}
	
	public Configer(String savePath){
		configFilePath =  savePath;
		configFilePath = System.getProperty("user.home");
		configFilePath = configFilePath + File.separator + "iptux.properties";
	}
	
	/**
	 * Save configuration data to config file.
	 */
	public void save(){
		
		String ipRanges = Arrays.toString(broadcastRanges);
		configer.setProperty("network_segment", ipRanges);
		configer.setProperty("encoding", msgEncoding);
		configer.setProperty("enter_key_send_message", String.valueOf(enterKeySend));
		configer.setProperty("save_chat_history", String.valueOf(saveChatHistory));
		configer.setProperty("file_save_path", fileSavePath);
		configer.setProperty("group_name", groupName);
		configer.setProperty("icon", icon);
		configer.setProperty("user_name", userName);
		configer.setProperty("chat_dialog_mid_Split_Postion", String.valueOf(midSplitPostion));
		configer.setProperty("chat_dialog_left_Split_Postion", String.valueOf(leftSplitPostion));
		FileOutputStream oFile;
		try {
			oFile = new FileOutputStream(configFilePath);
			configer.store(oFile, null);
			oFile.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Load configuration data from config file.
	 */
	public void load(){
		
		File file1 = new File(configFilePath);
		InputStream is;
		configer = new Properties();
		try {
			is = new FileInputStream(file1);
			configer.load(is);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		msgEncoding = configer.getProperty("encoding", "GB18030");
		enterKeySend =  Boolean.valueOf(configer.getProperty("enter_key_send_message", "true"));
		saveChatHistory =  Boolean.valueOf(configer.getProperty("save_chat_history", "false"));
		fileSavePath = configer.getProperty("file_save_path", System.getProperty("user.home"));
		userName = configer.getProperty("user_name", "");
		groupName = configer.getProperty("group_name", "");
		icon = configer.getProperty("icon", "");
		if(configer.getProperty("chat_dialog_mid_Split_Postion", "").equals("")){
			midSplitPostion = Integer.valueOf("130");
		}else{
			midSplitPostion = Integer.valueOf(configer.getProperty("chat_dialog_mid_Split_Postion", ""));
		}
		if(configer.getProperty("chat_dialog_left_Split_Postion", "").equals("")){
			leftSplitPostion = Integer.valueOf("150");
		}else{
			leftSplitPostion = Integer.valueOf(configer.getProperty("chat_dialog_left_Split_Postion", ""));
		}
		String tmp =  configer.getProperty("network_segment", "");
		tmp = tmp.replace("[", "");
		tmp = tmp.replace("]", "");
		if(!tmp.equals("") && (tmp != null) && !tmp.equals("null")){
			broadcastRanges = tmp.split(",");
			for (int i = 0; i < broadcastRanges.length; i++) {
				if (broadcastRanges[i].trim().equals("")) {
					broadcastRanges = Utils.deleteStringArrayItem(broadcastRanges, i);
				}
				// saved to file with toString, additional space will be add to
				// IP address
				// following lines to remove it.
				String startIP = broadcastRanges[i].split("-->")[0].trim();
				String endIP = broadcastRanges[i].split("-->")[1].trim();
				broadcastRanges[i] = startIP + "-->" + endIP;
			}
		}
		if(fileSavePath.equals("")){
			fileSavePath = System.getProperty("user.home")+File.separator+"iptuxDownload";
			File f = new File(fileSavePath);
			if(!f.exists()){
				f.mkdirs();
			}
		}
	}
	
	
}
